package com.example.zhaccount;

import java.util.ArrayList;
import java.util.List;

import android.app.Dialog;
import android.content.Intent;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.WindowManager.LayoutParams;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.Toast;

import com.example.activity.AddBillActivity;
import com.example.activity.AddNoteActivity;
import com.example.activity.CountActivity;
import com.example.adapter.ActPagerAdapter;
import com.example.adapter.ListViewAdapterBill;
import com.example.application.BaseActivity;
import com.example.application.MyApplication;
import com.example.bean.GridViewBean;
import com.example.db.DBService;
import com.slidingmenu.lib.SlidingMenu;

public class MainActivity extends BaseActivity implements OnClickListener {

    private MyApplication myApp;

    /**
     * SlideMenu 的相关变量
     */
    private SlidingMenu menu;
    private ImageView tv_menu;
    private TextView left_username, left_pass, left_help, left_about, left_import, left_exit, left_count;
    /**
     * viewPager的相关变量
     */
    private ViewPager mViewPager;
    private ArrayList<View> list;
    private LayoutInflater mInflater;
    private PopupWindow window;

    /**
     * popWindow 相关变量
     */
    private ImageView title_add;
    private TextView pop_income, pop_pay, pop_note;

    /**
     * Imgs 图标
     * 数据库 相关变量
     */
    private ListView listView;
    private ListViewAdapterBill mAdapter;
    private DBService dbService;
    private List<GridViewBean> mdbList;
    private Integer[] outImgs = {R.drawable.type_big_1, R.drawable.type_big_2,
            R.drawable.type_big_3, R.drawable.type_big_4, R.drawable.type_big_5,
            R.drawable.type_big_6, R.drawable.type_big_7, R.drawable.type_big_8,
            R.drawable.type_big_9, R.drawable.type_big_10, R.drawable.type_big_11,
            R.drawable.type_big_12};
    private Integer[] inImgs = {R.drawable.type_big_13, R.drawable.type_big_14, R.drawable.type_big_15,
            R.drawable.type_big_16, R.drawable.type_big_17, R.drawable.type_big_18, R.drawable.type_big_19};

    private int mCurrentIndex = 0;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mInflater = getLayoutInflater();
        myApp = (MyApplication) getApplication();

        mCurrentIndex = getIntent().getIntExtra("pagerIndex", 0);
        initView(mCurrentIndex);
        initSlideMenu();
        initDate();

    }

    public void initView(int index) {
        mViewPager = (ViewPager) findViewById(R.id.viewpager);
        list = new ArrayList<View>();
        //主页面-进入时的界面
        View v = mInflater.inflate(R.layout.fisrt_billdetail_lv, null);
        list.add(v);
        //第二个界面-笔记排列的页面
        list.add(mInflater.inflate(R.layout.second_notedetail_lv, null));
        //第三个页面-查看消费统计的页面
        list.add(mInflater.inflate(R.layout.thrid_acount_lv, null));
        ActPagerAdapter adapter = new ActPagerAdapter(list);
        mViewPager.setAdapter(adapter);
        if (index != 0) {
            mViewPager.setCurrentItem(index);
        } else {
            mViewPager.setCurrentItem(0);
        }

        title_add = (ImageView) v.findViewById(R.id.title_add);
        tv_menu = (ImageView) v.findViewById(R.id.title_menu);
        title_add.setOnClickListener(this);
        tv_menu.setOnClickListener(this);
        listView = (ListView) v.findViewById(R.id.listview);

    }


    public void initSlideMenu() {
        menu = new SlidingMenu(this);
        menu.setMode(SlidingMenu.LEFT);
        // 设置触摸屏幕的模式
        menu.setTouchModeAbove(SlidingMenu.LEFT);
        menu.setShadowWidthRes(R.dimen.shadow_width);

        // 设置滑动菜单视图的宽度
        menu.setBehindOffsetRes(R.dimen.slidingmenu_offset);
        int width = getWindowManager().getDefaultDisplay().getWidth();
        menu.setBehindWidth((int) (width * 0.4));
        // 设置渐出渐入效果的值
        menu.setFadeDegree(0.35f);

        menu.attachToActivity(this, SlidingMenu.SLIDING_CONTENT);
        // 为侧滑菜单设置布局
        menu.setMenu(R.layout.slidemenu_left);

        left_username = (TextView) menu.findViewById(R.id.left_username);
        left_pass = (TextView) menu.findViewById(R.id.left_pass);
        left_help = (TextView) menu.findViewById(R.id.left_help);
        left_about = (TextView) menu.findViewById(R.id.left_about);
        left_count = (TextView) menu.findViewById(R.id.left_count);
        left_import = (TextView) menu.findViewById(R.id.left_import);
        left_exit = (TextView) menu.findViewById(R.id.left_exit);

        left_username.setText(myApp.getUserName());
        left_pass.setOnClickListener(this);
        left_help.setOnClickListener(this);
        left_about.setOnClickListener(this);
        left_count.setOnClickListener(this);
        left_import.setOnClickListener(this);
        left_exit.setOnClickListener(this);

    }

    public void initDate() {
        dbService = new DBService(this);
        mdbList = new ArrayList<GridViewBean>();
        mdbList = dbService.findAllBillByName(myApp.getUserName());

        //判断是收入 还是支出图标
        for (GridViewBean bean : mdbList) {
            int state = Integer.parseInt(bean.getState().toString().trim());
            if (state == 1) {
                bean.setImg(outImgs[bean.getImg()]);
            } else if (state == 2) {
                bean.setImg(inImgs[bean.getImg()]);
            }
            Log.d("DBLIST",bean.toString());
        }
        mAdapter = new ListViewAdapterBill(MainActivity.this, mdbList);
        listView.setAdapter(mAdapter);
        listView.setOnItemClickListener(mAdapter);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.title_add:
                initPop();
                window.showAsDropDown(title_add, 0, 0);
                break;
            case R.id.title_menu:            //开关SlideMenu
                menu.toggle();
                break;
            case R.id.left_pass:            //显示更改密码界面
                showPass();
                break;
            case R.id.showpass_btn:            //更改密码
                updatePassword();
                break;
            case R.id.left_import:            //导入导出数据，备份
                break;
            case R.id.left_help:            //显示使用说明界面
                showHlepDialog();
                break;
            case R.id.left_about:            //显示关于界面
                showAbout();
                break;
            case R.id.left_exit:            //退出
                dbService.offDB();
                finish();
                break;
            case R.id.left_count:           //统计
                startActivity(new Intent(this, CountActivity.class));
                break;
            case R.id.pop_pay:                //支出
                Intent intent = new Intent(this, AddBillActivity.class);
                intent.putExtra("state", 1);
                startActivity(intent);
                break;
            case R.id.pop_income:            //收入
                Intent it = new Intent(this, AddBillActivity.class);
                it.putExtra("state", 2);
                startActivity(it);
                break;
            case R.id.pop_note:                //笔记
                Intent inte = new Intent(this, AddNoteActivity.class);
                startActivity(inte);
                break;
            default:
                break;
        }
    }

    //初始化PopWindow
    public void initPop() {
        View rootView = LayoutInflater.from(this)
                .inflate(R.layout.popwindow_layout, null);
        window = new PopupWindow(rootView,
                LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, true);
        rootView.setFocusable(true);
        window.setFocusable(true);
        window.setOutsideTouchable(true);
        window.setBackgroundDrawable(new BitmapDrawable());

        pop_income = (TextView) rootView.findViewById(R.id.pop_income);
        pop_pay = (TextView) rootView.findViewById(R.id.pop_pay);
        pop_note = (TextView) rootView.findViewById(R.id.pop_note);
        pop_income.setOnClickListener(this);
        pop_pay.setOnClickListener(this);
        pop_note.setOnClickListener(this);
    }

    //显示 使用说明界面
    private void showHlepDialog() {
        Dialog dialog = new Dialog(MainActivity.this, R.style.dialog);
        dialog.setContentView(R.layout.showhelpdialog);
        dialog.show();
    }

    //显示 关于界面
    private void showAbout() {
        Dialog dia = new Dialog(MainActivity.this, R.style.dialog);
        dia.setContentView(R.layout.showaboutdialog);
        dia.show();
    }

    private Dialog dd;

    //显示更改密码的界面
    private void showPass() {
        dd = new Dialog(MainActivity.this, R.style.dialog);
        dd.setContentView(R.layout.showpassword);
        dd.show();
        Button yes = (Button) dd.findViewById(R.id.showpass_btn);
        yes.setOnClickListener(this);
    }

    //更改密码
    private void updatePassword() {
        EditText oldPass = (EditText) dd.findViewById(R.id.show_oldPass);
        EditText newPass = (EditText) dd.findViewById(R.id.show_updatePass);
        String oldStr = oldPass.getText().toString();
        String newStr = newPass.getText().toString();
        if (oldStr != null && !oldStr.equals("")) {
            if (oldStr.equals(myApp.getPass()) && newStr != null
                    && !newStr.equals("") && !newStr.equals(oldPass)) {
                long num = dbService.updatePass(myApp.getUserName(), newStr);
                if (num != -1) {
                    dd.dismiss();
                    Toast.makeText(MainActivity.this, "更新密码成功", Toast.LENGTH_SHORT).show();
                } else {
                    Toast.makeText(MainActivity.this, "更新密码失败", Toast.LENGTH_SHORT).show();
                }
            } else {
                Toast.makeText(MainActivity.this, "请确保新密码不为空且两次密码不能相同", Toast.LENGTH_SHORT).show();
            }
        } else {
            Toast.makeText(MainActivity.this, "原始密码不能为空", Toast.LENGTH_SHORT).show();
        }
    }


}
